package com.api.gbhg.huangwm.web;

import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.huangwm.dao.ReimbursementListDao;
import com.customization.assets.util.DateTools;
import com.customization.commons.LogTool;
import com.engine.common.util.ParamUtil;
import jxl.read.biff.Record;
import weaver.conn.RecordSet;
import weaver.general.GCONST;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2022-04-24
 * @Version 1.0
 * @Description TODO 报销审核、查询页面api 报销记录api
 **/
@Path("/gb/huangwm/reimbursementList")
public class ReimbursementListApi {
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/web/ReimbursementListApi",false);
    private ReimbursementListDao dao = new ReimbursementListDao();
    private DateTools dateTools = new DateTools();

    public static void main(String[] args) {
        String rootPath = "D:\\WEAVER\\ecology\\";
        GCONST.setRootPath(rootPath);
        GCONST.setServerName("ecology");

        Map<String,Object> param = new HashMap<>();
        param.put("bmysjjfl","");
        param.put("dfczbkxm","");
        param.put("fzysrfl","7");
        param.put("pageNumber",1);
        param.put("zfysjjfl","");
        param.put("dygnkmbid","");
        param.put("pageSize",1000);
        param.put("kmjxj","");
        param.put("srlx","1");
        param.put("ysly","");
        param.put("shrq","2023-11-08");
        ReimbursementListApi api = new ReimbursementListApi();
        System.err.println(api.listV3(param));

    }
    @POST
    @Path("/unitList")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> unitTodo(@Context HttpServletResponse response, @Context HttpServletRequest request){
        Map<String, Object> apiResult = new HashMap<>();
        Map<String, Object> requestParam = ParamUtil.request2Map(request);
        List<Map<String,String>> result = null;
        try {
            result = dao.getDwshList(requestParam);
            String requestids = requestParam.get("requestids").toString();
            String requestname = requestParam.getOrDefault("requestname","").toString();
            String jbr = requestParam.getOrDefault("jbr","").toString();
            String jbks = requestParam.getOrDefault("jbks","").toString();
            String sqbxje1 = requestParam.getOrDefault("sqbxje1","").toString();
            String sqbxje2 = requestParam.getOrDefault("sqbxje2","").toString();
            if(sqbxje2.isEmpty() || Double.parseDouble(sqbxje2)==0.00) sqbxje2=sqbxje1;
            RecordSet rs = new RecordSet();
            String sql = "select lcid,sqbxje,requestid,wr.creater as jbr,wr.createdate as tdrq,requestname,requestmark as lcbh ,hr.departmentid,hr.subcompanyid1\n" +
                    "from workflow_requestbase wr right join(\n" +
                    "select lcid,sum(sqbxje) as sqbxje from uf_bxsjjl group by lcid\n" +
                    ")t on wr.requestid=t.lcid\n" +
                    "left join HrmResource hr on hr.id=wr.creater\n" +
                    "where t.lcid in("+requestids+") \n";
            if(!requestname.isEmpty()) sql+=" and requestname like '%"+requestname+"%' \n";
            if(!jbks.isEmpty()) sql+=" and departmentid = "+jbks+" \n";
            if(!jbr.isEmpty()) sql+=" and requestname creater="+jbr+" \n";
            if(!sqbxje1.isEmpty()) sql+=" and sqbxje between "+sqbxje1+" and "+sqbxje2+"\n";
            rs.execute(sql);
            apiResult.put("list",result);
            apiResult.put("count",rs.getCounts());
            apiResult.put("apiStatus",true);
        } catch (Exception e) {
            e.printStackTrace();
            apiResult.put("apiStatus",false);
            apiResult.put("msg",e.getMessage());
        }
        return apiResult;
    }

    @POST
    @Path("/listV3")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> listV3(Map<String, Object> requestParam){
        Map<String, Object> apiResult = new HashMap<>();
        logTool.writeLog("listV3参数："+JSONObject.toJSONString(requestParam));
        if(!requestParam.containsKey("srlx")){
            apiResult.put("code","500");
            apiResult.put("msg","参数缺少srlx");
            return apiResult;
        }
        List<Map<String,String>> result = new ArrayList<>();
        try {

            String srlx = requestParam.get("srlx").toString();
            String kmjxj = requestParam.get("kmjxj").toString();
            String dygnkmbid = requestParam.get("dygnkmbid").toString();
            String ysly = requestParam.get("ysly").toString();
            String fzysrfl = requestParam.get("fzysrfl").toString();
            String dfczbkxm = requestParam.get("dfczbkxm").toString();

            String bmysjjfl = requestParam.get("bmysjjfl").toString();
            String zfysjjfl = requestParam.get("zfysjjfl").toString();
            //2023年10月19日 增加审核日期参数
            String shrq = "";
            if(!requestParam.containsKey("shrq")){
                shrq = dateTools.getCurrentDate();
            }
            if(requestParam.containsKey("shrq")){
                if(requestParam.get("shrq").toString().isEmpty()){
                    shrq = dateTools.getCurrentDate();
                }else{
                    shrq = requestParam.get("shrq").toString();
                }
            }

            int pageNumber= Integer.parseInt(requestParam.get("pageNumber").toString());
            int pageSize= Integer.parseInt(requestParam.get("pageSize").toString());

            pageNumber=(pageNumber-1)*pageSize;

            String baseSql = "\nselect distinct uf_bxjl_kj.lcid from uf_bxjl_kj_dt1 left join uf_bxjl_kj on uf_bxjl_kj_dt1.lcid=uf_bxjl_kj.lcid \n";
            String sqlWhere = " where 1=1 and shrq<='"+shrq+"' \n" ;
            String sqlWhere2 = " where 1=1 and shrq<='"+shrq+"' \n" ;
            if(!srlx.isEmpty()){
                sqlWhere += " and zcqd="+srlx;
                sqlWhere2 += " and zcqd="+srlx;
            }
            if(!kmjxj.isEmpty()){
                sqlWhere += " and kmjxj="+kmjxj;
                sqlWhere2 += " and kmjxj="+kmjxj;
            }
            if(!dygnkmbid.isEmpty()){
                sqlWhere += " and dygnkmbid="+dygnkmbid;
                sqlWhere2 += " and dygnkmbid="+dygnkmbid;
            }

            if(!ysly.isEmpty()){
                if(ysly.equals("0") || ysly.indexOf("当年")>=0){
                    sqlWhere += " and ysly=0";
                    sqlWhere2 += " and ysly=0";
                }else{
                    sqlWhere += " and ysly=1";
                    sqlWhere2 += " and ysly=1";
                }
            }

            if(kmjxj.isEmpty() && fzysrfl.isEmpty()){
                apiResult.put("code","500");
                apiResult.put("msg","参数不允许kmjxj和fzysrfl同时为空");
                return apiResult;
            }

            if(!kmjxj.isEmpty() && !fzysrfl.isEmpty()){
                apiResult.put("code","500");
                apiResult.put("msg","参数不允许同时包含kmjxj和fzysrfl");
                return apiResult;
            }
            if(!fzysrfl.isEmpty()){
                sqlWhere += " and fzysrfl="+fzysrfl;
                sqlWhere2 += " and fzysrfl="+fzysrfl;
            }
            if(!dfczbkxm.isEmpty()){
                sqlWhere += " and zcdfczbkxm="+dfczbkxm;
                sqlWhere2 += " and zcdfczbkxm="+dfczbkxm;
            }
            if(!bmysjjfl.isEmpty()){
                sqlWhere += " and bmysjjfl="+bmysjjfl;
                sqlWhere2 += " and bmysjjfl="+bmysjjfl;
            }
            if(!zfysjjfl.isEmpty()){
                sqlWhere += " and zfysjjfl="+zfysjjfl;
                sqlWhere2 += " and zfysjjfl="+zfysjjfl;
            }
            baseSql += sqlWhere;
            logTool.writeLog("条件SQL："+baseSql);

            String sumSql = "SELECT ISNULL(SUM(uf_bxjl_kj_dt1.tybxje-ISNULL(uf_bxjl_kj_dt1.cxje,0)),0) AS tybxje FROM dbo.uf_bxjl_kj_dt1 \n" +
                    " left join uf_bxjl_kj on uf_bxjl_kj_dt1.lcid=uf_bxjl_kj.lcid "+sqlWhere2 +"  AND uf_bxjl_kj_dt1.lcid=t.lcid \n" +
                    "and ysxm in (select lcjlsqjlid from uf_jingfei_version where dlhsdw=1\n" +
                    "and ysnd="+ shrq.substring(0,4) +" ) ";
            //baseSql2用于查询总数
            String baseSql2 = "SELECT sqbxje,shrq,requestname from uf_bxjl_kj  a\n" +
                    "left join HrmResource b on b.id=a.jbr\n" +
                    "left join hrmdepartment c on c.id=a.jbks\n" +
                    "left join HrmSubcompany d on d.id=a.jbbm\n" +
                    "left join workflow_requestbase e on e.requestid=a.lcid\n" +
                    "where lcid in ("+baseSql+ " and ysxm in (select lcjlsqjlid from uf_jingfei_version where dlhsdw=1 \n" +
                    "and ysnd="+shrq.substring(0,4)+"))";
            //baseSql3用于查询数据
            String baseSql3 = "SELECT t.*,( "+sumSql+" ) as tybxje \n from ( select a.id,lcid,bxdbh,b.lastname,sqrq,departmentname,subcompanyname,\n" +
                    "sqbxje,shrq,requestname from uf_bxjl_kj  a\n" +
                    "left join HrmResource b on b.id=a.jbr\n" +
                    "left join hrmdepartment c on c.id=a.jbks\n" +
                    "left join HrmSubcompany d on d.id=a.jbbm\n" +
                    "left join workflow_requestbase e on e.requestid=a.lcid\n" +
                    "where lcid in ("+baseSql+ " and ysxm in (select lcjlsqjlid from uf_jingfei_version where dlhsdw=1 \n" +
                    "and ysnd="+shrq.substring(0,4)+"))";
            String getSql = baseSql3 + "\n  ) t order by lcid desc offset "+pageNumber+" rows fetch next "+pageSize+" rows only " ;
            logTool.writeLog("查询SQL："+getSql);
//            rs.execute(baseSql);
            RecordSet rs2 = new RecordSet();
            rs2.execute(getSql);
            while (rs2.next()){
                String lcid = rs2.getString("lcid");
//                String sql = "select lcid,bxdbh,b.lastname,sqrq,departmentname,subcompanyname,\n" +
//                        "sqbxje,tybxje,shrq from uf_bxjl_kj  a\n" +
//                        "left join HrmResource b on b.id=a.jbr\n" +
//                        "left join hrmdepartment c on c.id=a.jbks\n" +
//                        "left join HrmSubcompany d on d.id=a.jbbm\n" +
//                        "where lcid="+lcid;
//                rs2.execute(sql);
//                rs2.next();

                Map<String,String> data = new HashMap<>();
                data.put("lcid",lcid);
                data.put("requestname",rs2.getString("requestname"));
                data.put("bxdbh",rs2.getString("bxdbh"));
                data.put("lastname",rs2.getString("lastname"));
                data.put("sqrq",rs2.getString("sqrq"));
                data.put("departmentname",rs2.getString("departmentname"));
                data.put("subcompanyname",rs2.getString("subcompanyname"));
                data.put("sqbxje",rs2.getString("sqbxje"));
                data.put("tybxje",rs2.getString("tybxje"));
                data.put("shrq",rs2.getString("shrq"));
                result.add(data);
            }

            apiResult.put("data",result);
            RecordSet rs = new RecordSet();
            rs.execute(baseSql2);
            apiResult.put("count",rs.getCounts());
            apiResult.put("code","200");
        } catch (Exception e) {
            e.printStackTrace();
            apiResult.put("code","500");
            apiResult.put("msg",e.getMessage());
        }
        return apiResult;
    }



}
